package com.ling.common.library.base.mvp

import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import java.lang.ref.WeakReference

/**
 * @author Ling
 * @date 2024/3/25 13:58
 * @des mvp p层基类
 */
open class IPresenter<V : IView> {

    private var _mView : WeakReference<V>?= null
    protected val mView get() = _mView?.get()
    protected val mScope by lazy { MainScope() }
    //model must user manual create in user's presenter
    //because have activity not using model on some time

    open fun attach(view : V){
        this._mView = WeakReference(view)
    }

    open fun detach(){
        _mView?.clear()
        _mView = null
        mScope.cancel()
    }
}